let Const_URL = "holidayBenefits";
$(function () {
    let date = new Date();
    let y = date.getFullYear();
    let json = [{value: -1, text: "全部", selected: true}];
    for (let i = y; i >= 1990; i--) {
        json.push({value: i, text: i + '年节日福利'});
    }
    $('#cc').combobox({
        valueField: 'value',
        textField: 'text',
        data: json,
        limitToList: true
    });

    let year = -1;
    $("#search").on("click", function () {
        year = $('#cc').combobox('getValue');
        $('#benefits-dg').datagrid('load', {
            year: year,
            method: "findByUserId"
        });
    });
    $('#benefits-dg').datagrid({
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        scrollbarSize: 0,
        width: "auto",
        showFooter: true,
        url: Const_URL,
        onBeforeSelect: function () {
            return false;
        },
        queryParams: {
            year: year,
            method: "findByUserId"
        },
        columns: [[
            {field: 'remark', title: '备注', width: 25, align: 'center'},
            {field: 'salary', title: '福利金额', width: 15, align: 'right', halign: 'center'},
            {field: 'issueDate', title: '发放时间', width: 20, align: 'center'},
            {
                field: 'id', title: '操作', width: 10, formatter: function (value, row, index) {
                    let edit = '<button type="button" class="btn btn-primary btn-xs" name="edit" data-value="' + value + '" onclick="edit(this)"><i class="fa fa-pencil"></i>&nbsp;编辑</button>&nbsp;<button type="button" class="btn btn-danger btn-xs" name="del" data-value="' + value + '" onclick="deleteBenefits(this)"><i class="fa fa-trash"></i>&nbsp;删除</button>';
                    return value === 0 ? "" : edit;

                }, align: 'center'
            },
        ]],
        toolbar: [
            {
                iconCls: 'icon-add',
                text: "新增",
                handler: function () {
                    $("#add").window('open');
                    $("#iconCls").focus();
                }
            }
        ],
        onLoadSuccess: function (data) {
            let footer = [];
            let salary = [];
            $.each(data.rows, function (i, e) {
                footer.push(e.remark);
                salary.push(e.salaryNum);
                option.xAxis.data = footer;
                option.series[0].data = salary;
            });
        }
    });

    laydate.render({
        elem: "#issueDate",
        value: new Date()
    });

    laydate.render({
        elem: "#issueDateEdit",
        value: new Date()
    });

    $("#add").window({
        width: 600,
        height: 300,
        title: '新增福利',
        iconCls: 'icon-add',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#edit").window({
        width: 600,
        height: 300,
        title: '编辑福利',
        iconCls: 'icon-save',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#addBenefits").click(function () {
        let remark = $("#remark").val(),
            salary = $("#salary").val(),
            issueDate = $("#issueDate").val();
        if ($.trim(salary).length === 0) {
            $.messager.alert('错误信息', '福利金额不能为空', 'error');
            return;
        }
        if ($.trim(issueDate).length === 0) {
            $.messager.alert('错误信息', '发放日期不能为空', 'error');
            return;
        }
        if ($.trim(remark).length === 0) {
            $.messager.alert('错误信息', '备注不能为空', 'error');
            return;
        }

        $.ajax({
            type: "post",
            url: Const_URL,
            data: {
                remark: remark,
                salary: salary,
                issueDate: issueDate,
                method: "save"
            },
            dataType: "json",
            success: function (data) {
                if (data.status === 20000) {
                    layer.msg(data.msg, {icon: 1});
                    $("#add").window("close");
                    $('#benefits-dg').datagrid('reload');
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            }
        });
    });

    $("#editBenefits").click(function () {
        let id = $.trim($("#benefitsId").val()),
            remark = $("#remarkEdit").val(),
            salary = $("#salaryEdit").val(),
            issueDate = $("#issueDateEdit").val();
        if ($.trim(salary).length === 0) {
            $.messager.alert('错误信息', '福利金额不能为空', 'error');
            return;
        }
        if ($.trim(issueDate).length === 0) {
            $.messager.alert('错误信息', '发放日期不能为空', 'error');
            return;
        }
        if ($.trim(remark).length === 0) {
            $.messager.alert('错误信息', '备注不能为空', 'error');
            return;
        }
        let json = {
            id: id,
            salary: salary,
            issueDate: issueDate,
            remark: remark,
            method: "update"
        };
        $.post(Const_URL, json, function (data) {
            if (data.status === 20000) {
                layer.msg(data.msg, {icon: 1});
                $("#edit").window("close");
                $('#benefits-dg').datagrid('reload');
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        }, "json");
    });
});

function edit(obj) {
    let id = $(obj).attr("data-value");
    $("#edit").window('open');
    $.ajax({
        type: "get",
        url: Const_URL + "?method=findById&id=" + id,
        dataType: "json",
        success: function (data) {
            $("#benefitsId").val(data.id);
            $("#salaryEdit").val(data.salaryNum);
            $("#issueDateEdit").val(data.issueDate);
            $("#remarkEdit").val(data.remark);
        }
    });
}

function deleteBenefits(obj) {
    layer.confirm("确认删除该条数据吗？", {icon: 3, title: "删除确认"}, function (index) {
        let id = $(obj).attr("data-value");
        $.ajax({
            type: "get",
            url: Const_URL + "?method=delete&id=" + id,
            dataType: "json",
            success: function (data) {
                if (data.status === 20000) {
                    layer.msg(data.msg, {icon: 1});
                    $('#benefits-dg').datagrid('reload');
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            }
        });
        layer.close(index);
    });
}